package manager;

import com.example.xin_game_car.R;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.RectF;

public class CarManager {

	Bitmap bitmap_car,bitmap_car_right,bitmap_car_left;
	Context mContext;
	float left, top,mScreenWidth,mScreenHeight;
	boolean isRight = false,isLeft = false;
	
	public CarManager(Context pContext,float screenWidth,float screenHeight) {
		mContext = pContext;
		bitmap_car = BitmapFactory.decodeResource(mContext.getResources(), R.drawable.car);
		bitmap_car_right = BitmapFactory.decodeResource(mContext.getResources(), R.drawable.car_right);
		bitmap_car_left = BitmapFactory.decodeResource(mContext.getResources(), R.drawable.car_left);
		left = screenWidth*2/5;
		top = screenHeight*5/6;
		mScreenWidth = screenWidth;
		mScreenHeight = screenHeight;
	}
	
	
	public void drawCar(Canvas pCanvas){
		if(isRight){			
			pCanvas.drawBitmap(bitmap_car_right, null, new RectF(left, top, left+mScreenWidth/5, top+mScreenWidth/5*2/3), null);
		}else if(isLeft){
			pCanvas.drawBitmap(bitmap_car_left, null, new RectF(left, top, left+mScreenWidth/5, top+mScreenWidth/5*2/3), null);
		}else{
			//pCanvas.drawBitmap(bitmap_car, null, new RectF(left, top, left+mScreenWidth/12, mScreenWidth/12*2/3), null);
			pCanvas.drawBitmap(bitmap_car, null, new RectF(left, top, left+mScreenWidth/5,top+mScreenWidth/5*2/3), null);
			
		}
	}
	
	public void moveCar(int x){
		//System.out.println("x="+x);
		if(x<0){
			if(left<mScreenWidth-mScreenWidth/5){				
				left = left+50;
			}
			isRight = true;
			isLeft = false;
		}else if(x>0){
			if(left>0){				
				left = left-50;
			}
			isRight = false;
			isLeft = true;
		}else{
			isRight = false;
			isLeft = false;
		}
		
	}
	
}
